package cn.yzstu.webutil.api;

import cn.yzstu.webutil.service.DouYinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DouYinApi
 * @Description
 * @Author wangyong
 * @Modified By:
 * @Date 2020-09-03 17:14
 */
@Api(value = "抖音相关Api")
@RestController
@Deprecated
@RequestMapping(value = "/v1/api/douyin")
public class DouYinApi {

    @Autowired
    private DouYinService douYinService;

    @GetMapping("/getUrl")
    @ApiOperation(value = "解析抖音链接")
    public Map<String, Object> getUrl(@RequestParam(name = "url") @ApiParam() String url, @RequestParam(name = "mode", required = false) String mode) {

        HashMap<String, Object> result = new HashMap<>();

        try {
            url = url.replaceAll("\\s+", "");
            if (url.matches("^(#在抖音，记录美好生活#).*https://.*/.*复制此链接，打开【抖音短视频】，直接观看视频！")) {
                url = url.substring(url.indexOf("https://"), url.lastIndexOf('/') + 1);
            } else if (url.matches("^(https://).*/")) {

            } else {
                throw new RuntimeException("url incorrect format.");
            }
            result.put("code", 0);
            result.put("msg", "success");


            if (mode != null) {
                if (mode.equals("1")) {
                    result.put("url", douYinService.mode1(url));
                } else {
                    result.put("url", douYinService.mode2(url));
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 1);
            result.put("msg", "analysis fail. reason: " + e.getLocalizedMessage());
            return result;
        }

    }

}
